/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.vote.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import com.wealth.vote.entity.CooperativeEmp;
import com.wealth.vote.repository.CooperativeEmpDao;
import com.wealth.vote.service.ICooperativeEmpService;

import iaas.enums.StatusEnum;
import iaas.persistence.DynamicSpecifications;
import iaas.persistence.SearchFilter;
import iaas.persistence.SearchFilter.Operator;
import iaas.service.impl.BaseServiceImpl;
import iaas.utils.ResultObject;

/**
 * @author chan
 * @version 1.0
 * @since 1.0
 */
 
@Component
public class CooperativeEmpServiceImpl extends BaseServiceImpl<CooperativeEmp> implements ICooperativeEmpService{

	@Autowired
	private CooperativeEmpDao cooperativeEmpDao;

	@Override
	public CooperativeEmp getEmpByEmpNo(String empNo) throws Exception {
		List<CooperativeEmp> empList = cooperativeEmpDao.findByEmpNoAndStatus(empNo, StatusEnum.NORMAL.getValue());
		if(empList.size()>0){
			return empList.get(0);
		}else{
			return null;
		}
	}

	@Override
	public ResultObject getEmpPageData(Integer pageSize, Integer pageNumber, Long cooperId) throws Exception {
		/** ---- 分页查询 ---*/
		//创建分页请求.
		Sort sort = new Sort(Direction.DESC,"votes");
		PageRequest pageRequest = new PageRequest(pageNumber-1, pageSize, sort);
		//创建动态查询条件组合.
		Collection<SearchFilter> filters = new ArrayList<SearchFilter>();
		//过滤:会员
		if(cooperId!=null){
			filters.add(new SearchFilter("cooprt.id", Operator.EQ, cooperId));
		}
		filters.add(new SearchFilter("status", Operator.EQ,1));
		Specification<CooperativeEmp> spec = DynamicSpecifications.bySearchFilter(filters, CooperativeEmp.class);
		Page<CooperativeEmp> page  = cooperativeEmpDao.findAll(spec, pageRequest);
		Long count= page.getTotalElements();
		List<CooperativeEmp> empList = page.getContent();
		return new ResultObject("000", String.valueOf(count), empList);
	}

	@Override
	public List<CooperativeEmp> getEmpByStatus() throws Exception {
		return cooperativeEmpDao.findByStatusOrderByVotesDesc(StatusEnum.NORMAL.getValue());
	}
	
}
